package com.itheima.dao;

import com.itheima.domain.Manager;

import java.io.*;
import java.util.ArrayList;

public class ImplManagerDao implements IManagerDao {
    private static ArrayList<Manager> manlist = new ArrayList<>();

    @Override
    public int manLogin(String id, String password) {
       if (manlist.size()==0){
           return -1;
       } for (Manager manager : manlist) {
            if (id.equals(manager.getId())){
                if (password.equals(manager.getPassword())){
                    return 0;
                }
                return 1;
            }
        }
        return 2;
    }

    @Override
    public void register(Manager manger) throws IOException {
        manlist.add(manger);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ItHeiMaSystem\\myStreamFile/manager.txt"));
        oos.writeObject(manger);
        oos.close();
    }

    @Override
    public void updateMan( String id,Manager manager)  {
        for (int i = 0; i < manlist.size(); i++) {
            if (id.equals(manlist.get(i).getId())){
                manlist.set(i,manager);
                break;
            }
        }
    }

    @Override
    public ArrayList<Manager> findAll() {
        return manlist;
    }

    @Override
    public Manager findMan(String id) {
        for (Manager manager : manlist) {
            if (id.equals(manager.getId())){
              return manager;
            }
        }
        return null;
    }

    @Override
    public ArrayList<String> lookLogger() throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedReader bw = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\log4j-log.log")));
        String line;
        while ((line = bw.readLine()) != null) {
            list.add(line);
        }
        return list;
    }
}
